/**
 * 一个字符串s可能包含{}()[]三种括号
 * 判断s是否是括号匹配的
 * 如(a{b}c)匹配，而{a(b 或 {a{b}c}就不匹配
 */

function matchBreaket(s: string){
    if(s.length === 0){
        return true;
    }
    const leftStr: string = '([{';
    const rightStr: string = ')]}';
    const stack: any = [];

    for(let i = 0 ; i < s.length ; i++){
        if(leftStr.includes(s[i])){
            console.log('left:',s[i]);
            stack.push(s[i]);
        }else if(rightStr.includes(s[i])){
            const top = stack[stack.length-1];//栈顶元素
            console.log("right: ",s[i]);
            console.log("top:",top,"s[i]:",s[i])
            if( isMatch(top,s[i])){
                console.log("top:",top);
                stack.pop();//左右括号匹配，则出栈
            }
        }
    }
    console.log("stack:",stack);
    return stack.length === 0;//栈内为空，则匹配
}
function isMatch(left: string, right: string){
    if(left === '(' && right === ')') return true;
    if(left === '{' && right === '}') return true;
    if(left === '[' && right === ']') return true;
}

 const str1 = '{a(b[c]d)e}';
 const str2 = '(1[[{1]2)3}';
 console.log(matchBreaket(str1));
 console.log(matchBreaket(str2));